package org.rabbitmq.provider.rabbitmq.support;

import java.util.UUID;

import org.rabbitmq.common.enums.ExchangeEnum;
import org.rabbitmq.common.enums.QueueEnum;
import org.rabbitmq.provider.rabbitmq.QueueMessageService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
*@author LSY
*Time: 2018年12月6日上午10:58:14
*
*
*/
@Component
public class QueueMessageServiceSupport implements QueueMessageService{

	@Autowired
	private RabbitTemplate rabbitTemplate;
	
	
	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String cause) {

		System.out.println("回调id：" + correlationData.getId());
		if(ack) {
			System.out.println("消息发送成功");
		} else {
			System.out.println("消息发送失败:" + cause);
		}
	}

	@Override
	public void send(Object message, ExchangeEnum exchangeEnum, QueueEnum queueEnum) throws Exception {

		//设置回调当前类对象
		rabbitTemplate.setConfirmCallback(this);
		//构建回调id为UUID
		CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
		//发送消息到消息队列
		rabbitTemplate.convertAndSend(exchangeEnum.getValue(), queueEnum.getRoutingKey(), message, correlationId);
	}

}
